home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / gdb / c-valpri.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  11.4 KB  |  422 lines

  1. /* Support for printing C values for GDB, the GNU debugger.
  2.    Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "symtab.h"
  22. #include "gdbtypes.h"
  23. #include "expression.h"
  24. #include "value.h"
  25. #include "demangle.h"
  26. #include "valprint.h"
  27. #include "language.h"
  28.  
  29. /* BEGIN-FIXME */
  30.  
  31. extern int vtblprint;        /* Controls printing of vtbl's */
  32. extern int demangle;        /* whether to print C++ syms raw or src-form */
  33.  
  34. extern void
  35. cp_print_class_member PARAMS ((char *, struct type *, GDB_FILE *, char *));
  36.  
  37. extern void
  38. cp_print_class_method PARAMS ((char *, struct type *, GDB_FILE *));
  39.  
  40. extern void
  41. cp_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int,
  42.                    enum val_prettyprint, struct type **));
  43.  
  44. extern int
  45. cp_is_vtbl_ptr_type PARAMS ((struct type *));
  46.  
  47. extern int
  48. cp_is_vtbl_member PARAMS ((struct type *));
  49.  
  50. /* END-FIXME */
  51.  
  52.  
  53. /* BEGIN-FIXME:  Hooks into c-typeprint.c */
  54.  
  55. extern void
  56. c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int));
  57.  
  58. extern void
  59. cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
  60.                    GDB_FILE *));
  61. /* END-FIXME */
  62.  
  63.  
  64. extern struct obstack dont_print_obstack;
  65.  
  66.  
  67. /* Print data of type TYPE located at VALADDR (within GDB), which came from
  68.    the inferior at address ADDRESS, onto stdio stream STREAM according to
  69.    FORMAT (a letter or 0 for natural format).  The data at VALADDR is in
  70.    target byte order.
  71.  
  72.    If the data are a string pointer, returns the number of string characters
  73.    printed.
  74.  
  75.    If DEREF_REF is nonzero, then dereference references, otherwise just print
  76.    them like pointers.
  77.  
  78.    The PRETTY parameter controls prettyprinting.  */
  79.  
  80. int
  81. c_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
  82.          pretty)
  83.      struct type *type;
  84.      char *valaddr;
  85.      CORE_ADDR address;
  86.      GDB_FILE *stream;
  87.      int format;
  88.      int deref_ref;
  89.      int recurse;
  90.      enum val_prettyprint pretty;
  91. {
  92.   register unsigned int i = 0;        /* Number of characters printed */
  93.   unsigned len;
  94.   struct type *elttype;
  95.   unsigned eltlen;
  96.   LONGEST val;
  97.   CORE_ADDR addr;
  98.  
  99.   switch (TYPE_CODE (type))
  100.     {
  101.     case TYPE_CODE_ARRAY:
  102.       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
  103.     {
  104.       elttype = TYPE_TARGET_TYPE (type);
  105.       eltlen = TYPE_LENGTH (elttype);
  106.       len = TYPE_LENGTH (type) / eltlen;
  107.       if (prettyprint_arrays)
  108.         {
  109.           print_spaces_filtered (2 + 2 * recurse, stream);
  110.         }
  111.       /* For an array of chars, print with string syntax.  */
  112.       if (eltlen == 1 && TYPE_CODE (elttype) == TYPE_CODE_INT
  113.           && (format == 0 || format == 's'))
  114.         {
  115.           LA_PRINT_STRING (stream, valaddr, len, 0);
  116.           i = len;
  117.         }
  118.       else
  119.         {
  120.           fprintf_filtered (stream, "{");
  121.           /* If this is a virtual function table, print the 0th
  122.          entry specially, and the rest of the members normally.  */
  123.           if (cp_is_vtbl_ptr_type (elttype))
  124.         {
  125.           i = 1;
  126.           fprintf_filtered (stream, "%d vtable entries", len - 1);
  127.         }
  128.           else
  129.         {
  130.           i = 0;
  131.         }
  132.           val_print_array_elements (type, valaddr, address, stream,
  133.                     format, deref_ref, recurse, pretty, i);
  134.           fprintf_filtered (stream, "}");
  135.         }
  136.       break;
  137.     }
  138.       /* Array of unspecified length: treat like pointer to first elt.  */
  139.       addr = address;
  140.       goto print_unpacked_pointer;
  141.  
  142.     case TYPE_CODE_PTR:
  143.       if (format && format != 's')
  144.     {
  145.       print_scalar_formatted (valaddr, type, format, 0, stream);
  146.       break;
  147.     }
  148.       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
  149.     {
  150.       cp_print_class_method (valaddr, type, stream);
  151.     }
  152.       else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
  153.     {
  154.       cp_print_class_member (valaddr,
  155.                  TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
  156.                  stream, "&");
  157.     }
  158.       else
  159.     {
  160.       addr = unpack_pointer (type, valaddr);
  161.     print_unpacked_pointer:
  162.       elttype = TYPE_TARGET_TYPE (type);
  163.  
  164.       if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
  165.         {
  166.           /* Try to print what function it points to.  */
  167.           print_address_demangle (addr, stream, demangle);
  168.           /* Return value is irrelevant except for string pointers.  */
  169.           return (0);
  170.         }
  171.  
  172.       if (addressprint && format != 's')
  173.         {
  174.           fprintf_filtered (stream, "0x%lx", (unsigned long)addr);
  175.         }
  176.  
  177.       /* For a pointer to char or unsigned char, also print the string
  178.          pointed to, unless pointer is null.  */
  179.       if (TYPE_LENGTH (elttype) == 1
  180.           && TYPE_CODE (elttype) == TYPE_CODE_INT
  181.           && (format == 0 || format == 's')
  182.           && addr != 0)
  183.         {
  184.           i = val_print_string (addr, 0, stream);
  185.         }
  186.       else if (cp_is_vtbl_member(type))
  187.           {
  188.           /* print vtbl's nicely */
  189.           CORE_ADDR vt_address = unpack_pointer (type, valaddr);
  190.  
  191.           struct minimal_symbol *msymbol =
  192.         lookup_minimal_symbol_by_pc (vt_address);
  193.           if ((msymbol != NULL) &&
  194.           (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
  195.         {
  196.           fputs_filtered (" <", stream);
  197.           fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream);
  198.           fputs_filtered (">", stream);
  199.         }
  200.           if (vt_address && vtblprint)
  201.             {
  202.           value vt_val;
  203.               struct symbol *wsym = (struct symbol *)NULL;
  204.               struct type *wtype;
  205.           struct symtab *s;
  206.           struct block *block = (struct block *)NULL;
  207.           int is_this_fld;
  208.  
  209.           if (msymbol != NULL)
  210.                       wsym = lookup_symbol (SYMBOL_NAME(msymbol), block, 
  211.                 VAR_NAMESPACE, &is_this_fld, &s);
  212.  
  213.           if (wsym)
  214.             {
  215.                   wtype = SYMBOL_TYPE(wsym);
  216.             }
  217.           else
  218.             {
  219.               wtype = TYPE_TARGET_TYPE(type);
  220.             }
  221.           vt_val = value_at (wtype, vt_address);
  222.           val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val),
  223.                  VALUE_ADDRESS (vt_val), stream, format,
  224.                  deref_ref, recurse + 1, pretty);
  225.           if (pretty)
  226.             {
  227.               fprintf_filtered (stream, "\n");
  228.               print_spaces_filtered (2 + 2 * recurse, stream);
  229.             }
  230.             }
  231.           }
  232.  
  233.       /* Return number of characters printed, plus one for the
  234.          terminating null if we have "reached the end".  */
  235.       return (i + (print_max && i != print_max));
  236.     }
  237.       break;
  238.  
  239.     case TYPE_CODE_MEMBER:
  240.       error ("not implemented: member type in c_val_print");
  241.       break;
  242.  
  243.     case TYPE_CODE_REF:
  244.       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER)
  245.         {
  246.       cp_print_class_member (valaddr,
  247.                  TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
  248.                  stream, "");
  249.       break;
  250.     }
  251.       if (addressprint)
  252.         {
  253.       fprintf_filtered (stream, "@0x%lx",
  254.                   unpack_long (builtin_type_int, valaddr));
  255.       if (deref_ref)
  256.         fputs_filtered (": ", stream);
  257.         }
  258.       /* De-reference the reference.  */
  259.       if (deref_ref)
  260.     {
  261.       if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
  262.         {
  263.           value deref_val =
  264.         value_at
  265.           (TYPE_TARGET_TYPE (type),
  266.            unpack_pointer (lookup_pointer_type (builtin_type_void),
  267.                    valaddr));
  268.           val_print (VALUE_TYPE (deref_val),
  269.              VALUE_CONTENTS (deref_val),
  270.              VALUE_ADDRESS (deref_val), stream, format,
  271.              deref_ref, recurse + 1, pretty);
  272.         }
  273.       else
  274.         fputs_filtered ("???", stream);
  275.     }
  276.       break;
  277.  
  278.     case TYPE_CODE_UNION:
  279.       if (recurse && !unionprint)
  280.     {
  281.       fprintf_filtered (stream, "{...}");
  282.       break;
  283.     }
  284.       /* Fall through.  */
  285.     case TYPE_CODE_STRUCT:
  286.       if (vtblprint && cp_is_vtbl_ptr_type(type))
  287.     {
  288.           /* Print the unmangled name if desired.  */
  289.       print_address_demangle(*((int *) (valaddr +    /* FIXME bytesex */
  290.           TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8)),
  291.           stream, demangle);
  292.       break;
  293.     }
  294.       cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
  295.                  0);
  296.       break;
  297.  
  298.     case TYPE_CODE_ENUM:
  299.       if (format)
  300.     {
  301.       print_scalar_formatted (valaddr, type, format, 0, stream);
  302.       break;
  303.     }
  304.       len = TYPE_NFIELDS (type);
  305.       val = unpack_long (type, valaddr);
  306.       for (i = 0; i < len; i++)
  307.     {
  308.       QUIT;
  309.       if (val == TYPE_FIELD_BITPOS (type, i))
  310.         {
  311.           break;
  312.         }
  313.     }
  314.       if (i < len)
  315.     {
  316.       fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
  317.     }
  318.       else
  319.     {
  320.       print_longest (stream, 'd', 0, val);
  321.     }
  322.       break;
  323.  
  324.     case TYPE_CODE_FUNC:
  325.       if (format)
  326.     {
  327.       print_scalar_formatted (valaddr, type, format, 0, stream);
  328.       break;
  329.     }
  330.       /* FIXME, we should consider, at least for ANSI C language, eliminating
  331.      the distinction made between FUNCs and POINTERs to FUNCs.  */
  332.       fprintf_filtered (stream, "{");
  333.       type_print (type, "", stream, -1);
  334.       fprintf_filtered (stream, "} ");
  335.       /* Try to print what function it points to, and its address.  */
  336.       print_address_demangle (address, stream, demangle);
  337.       break;
  338.  
  339.     case TYPE_CODE_BOOL:
  340.       /* Do something at least vaguely reasonable, for example if the
  341.      language is set wrong.  */
  342.  
  343.     case TYPE_CODE_RANGE:
  344.       /* FIXME: create_range_type does not set the unsigned bit in a
  345.      range type (I think it probably should copy it from the target
  346.      type), so we won't print values which are too large to
  347.      fit in a signed integer correctly.  */
  348.       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
  349.      print with the target type, though, because the size of our type
  350.      and the target type might differ).  */
  351.       /* FALLTHROUGH */
  352.  
  353.     case TYPE_CODE_INT:
  354.       format = format ? format : output_format;
  355.       if (format)
  356.     {
  357.       print_scalar_formatted (valaddr, type, format, 0, stream);
  358.     }
  359.       else
  360.     {
  361.       val_print_type_code_int (type, valaddr, stream);
  362.       /* C and C++ has no single byte int type, char is used instead.
  363.          Since we don't know whether the value is really intended to
  364.          be used as an integer or a character, print the character
  365.          equivalent as well. */
  366.       if (TYPE_LENGTH (type) == 1)
  367.         {
  368.           fputs_filtered (" ", stream);
  369.           LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
  370.                  stream);
  371.         }
  372.     }
  373.       break;
  374.  
  375.     case TYPE_CODE_CHAR:
  376.       format = format ? format : output_format;
  377.       if (format)
  378.     {
  379.       print_scalar_formatted (valaddr, type, format, 0, stream);
  380.     }
  381.       else
  382.     {
  383.       fprintf_filtered (stream, TYPE_UNSIGNED (type) ? "%u" : "%d",
  384.                 unpack_long (type, valaddr));
  385.       fputs_filtered (" ", stream);
  386.       LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr), stream);
  387.     }
  388.       break;
  389.  
  390.     case TYPE_CODE_FLT:
  391.       if (format)
  392.     {
  393.       print_scalar_formatted (valaddr, type, format, 0, stream);
  394.     }
  395.       else
  396.     {
  397.       print_floating (valaddr, type, stream);
  398.     }
  399.       break;
  400.  
  401.     case TYPE_CODE_VOID:
  402.       fprintf_filtered (stream, "void");
  403.       break;
  404.  
  405.     case TYPE_CODE_ERROR:
  406.       fprintf_filtered (stream, "<error type>");
  407.       break;
  408.  
  409.     case TYPE_CODE_UNDEF:
  410.       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
  411.      dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
  412.      and no complete type for struct foo in that file.  */
  413.       fprintf_filtered (stream, "<incomplete type>");
  414.       break;
  415.  
  416.     default:
  417.       error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
  418.     }
  419.   gdb_flush (stream);
  420.   return (0);
  421. }
  422.